package com.mt.sdk.ble.base;

import java.util.UUID;
import com.mt.sdk.ble.MTBLEDevice;
import com.mt.sdk.ble.MTBLEManager;
import com.mt.sdk.ble.model.BLEBaseAction;
import com.mt.sdk.ble.model.ErroCode;
import com.mt.sdk.ble.model.MTBeaconInfModel;
import com.mt.sdk.ble.model.WriteCharactWithAckAction;
import com.mt.sdk.ble.model.WriteDescriptorAction;
import com.mt.sdk.longdatas.protocol.MTBeacon4Protocol;
import com.mt.sdk.ble.model.BLEBaseAction.ActionStatues;
import com.mt.sdk.ble.model.BLEBaseAction.Option;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.os.Build;
import android.os.Handler;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class MTBeacon4Base extends BLEBase {
	private static final int MAXPRIVATEID = 3;

	public MTBeacon4Base(Context context, MTBLEManager manager) {
		super(context, manager);
	}

	private Handler handl = new Handler();

	private MTBLEDevice device;
	private String pwd;
	public static String[] SENDPOWERNAME = new String[] { "A", "B", "C", "D" };

	// 连接
	public ErroCode connect(MTBLEDevice device, String pwd, int retryTimes, boolean isScan, CallBack callback) {
		this.callback = callback;
		this.device = device;
		this.pwd = pwd;
		return super.connect(device.getDevice().getAddress(), retryTimes, isScan);
	}

	// 获取特定服务
	private static final String DATA_SERVICE_UUID = "0000f8f0-0000-1000-8000-00805f9b34fb";
	private static final String TXD_CHARACT_UUID = "0000f8f1-0000-1000-8000-00805f9b34fb";
	private static final String RXD_CHARACT_UUID = "0000f8f2-0000-1000-8000-00805f9b34fb";
	private BluetoothGattCharacteristic txd_charact;
	private BluetoothGattCharacteristic rxd_charact;

	private boolean getCharacts() {
		BluetoothGattService data_service = getService(DATA_SERVICE_UUID);
		if (data_service == null) { // 服务获取失败的情况
			return false;
		}

		txd_charact = data_service.getCharacteristic(UUID.fromString(TXD_CHARACT_UUID));
		rxd_charact = data_service.getCharacteristic(UUID.fromString(RXD_CHARACT_UUID));

		if ((txd_charact == null) || (rxd_charact == null)) {
			return false;
		}

		return true;
	}

	// 使能特征值的可通知属性
	private boolean enableNotify() {
		addListCharact(rxd_charact);
		enableNotify(rxd_charact, true);
		BluetoothGattDescriptor descriptor = rxd_charact
				.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
		descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
		addNewAction(new WriteDescriptorAction(descriptor, new BLEBaseAction.Option(2000)) {
			@Override
			public void onSuccess() {
				super.onSuccess();
				if (device.getSetlev() == 0) { // 可正常连接
					if (callback != null) {
						callback.onConnect(ErroCode.ERROMAP.get("OK"));
					}
					return;
				}

				final MTBeacon4Protocol protocol = new MTBeacon4Protocol();
				protocol.setCallBack(new MTBeacon4Protocol.MTBeacon4ProtocolCallBack() {

					@Override
					public void unKnowDatas(byte[] cmd) {

					}

					@Override
					public void onVerifyPwd(boolean statues, int reason) {
						action.setStatues(ActionStatues.DONE);
						if (!statues) {
							if (callback != null) {
								callback.onConnect(ErroCode.ERROMAP.get("pwderro"));
							}
							return;
						}

						if (callback != null) {
							callback.onConnect(ErroCode.ERROMAP.get("OK"));
						}
					}

					@Override
					public void onSetPublicBeaconParams(boolean statues, int reason) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSetPrivateBeaconParams(boolean statues, int reason) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSetDeverlop(boolean statues, int reason) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onGetVersion(String version, boolean statues, int reason) {
					}

					@Override
					public void onGetPublicBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
					}

					@Override
					public void onGetPrivateBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
					}
				});

				if (device.getSetlev() == 1) { // 密码部署,需要密码验证
					System.out.println("密码部署,需要密码验证->" + pwd);
					action = new WriteCharactWithAckAction(txd_charact, rxd_charact, MTBeacon4Protocol.verifyPwd(pwd),
							new Option(1000)) {

						@Override
						public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
							super.onReciveDatas(characteristic, datas);
							protocol.reviceDatas(datas);
						}

						@Override
						public void onSuccess() {
							super.onSuccess();
						}

						@Override
						public void onFail(ErroCode erro) {
							super.onFail(erro);
							disConnect();
							if (callback != null) {
								callback.onConnect(ErroCode.ERROMAP.get("pwdfail"));
							}
						}
					};
					addNewAction(action);
					return;
				}
			}

			@Override
			public void onFail(ErroCode erro) {
				super.onFail(erro);
				if (callback != null) {
					callback.onConnect(ErroCode.ERROMAP.get("enablenotifyerro"));
				}
				disConnect();
			}

		});

		return true;
	}

	@Override
	final protected void onBaseConnect(ErroCode errocode) {
		super.onBaseConnect(errocode);
		if (errocode.getCode() != 0) { // 连接失败的情况
			if (callback != null) {
				callback.onConnect(errocode);
			}
			disConnect();
			return;
		}

		if (!getCharacts()) { // 获取特定服务失败的情况
			if (callback != null) {
				callback.onConnect(ErroCode.ERROMAP.get("getcharacterro"));
			}
			disConnect();
			return;
		}

		if (!enableNotify()) { // 使能可通知失败的情况
			System.out.println("enableNotify");
			if (callback != null) {
				callback.onConnect(ErroCode.ERROMAP.get("enablenotifyerro"));
			}
			disConnect();
			return;
		}
	}

	@Override
	final protected void reTryConnect(int lasttimes) {
		super.reTryConnect(lasttimes);
		if (callback != null) {
			callback.reTryConnect(lasttimes);
		}
	}

	@Override
	final protected void onDisConnect(ErroCode errocode) {
		super.onDisConnect(errocode);
		if (callback != null) {
			callback.onDisConnect(errocode);
		}
	}

	@Override
	final protected void onManualDisConnect(ErroCode errocode) {
		super.onManualDisConnect(errocode);
	}

	// 读取所有参数
	private BLEBaseAction action;

	public boolean readAllInf(final MTBLEDevice device, final String pwd, final ReadInfsCallBack readinfscallback) {
		readid = 0;
		final MTBeacon4Protocol protocol = new MTBeacon4Protocol();
		protocol.setCallBack(new MTBeacon4Protocol.MTBeacon4ProtocolCallBack() {

			@Override
			public void unKnowDatas(byte[] cmd) {

			}

			@Override
			public void onVerifyPwd(boolean statues, int reason) {}

			@Override
			public void onSetPublicBeaconParams(boolean statues, int reason) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSetPrivateBeaconParams(boolean statues, int reason) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSetDeverlop(boolean statues, int reason) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetVersion(String version, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				if (!statues) {
					disConnect();
					if (readinfscallback != null) {
						readinfscallback.onFail(ErroCode.ERROMAP.get("readversionderro"));
					}
					return;
				}
				if (publicdeviceinf == null) {
					disConnect();
					return;
				}

				publicdeviceinf.setVersion(version);

				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.getPrivateBeaconParams(readid), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (readinfscallback != null) {
							readinfscallback.onFail(ErroCode.ERROMAP.get("readprivateparamsfail"));
						}
					}
				};
				addNewAction(action);
			}

			@Override
			public void onGetPublicBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				System.out.println("onGetPublicBeaconParams");
				if (!statues) {
					disConnect();
					if (readinfscallback != null) {
						readinfscallback.onFail(ErroCode.ERROMAP.get("readpublicparamsfail"));
					}
					return;
				}
				publicdeviceinf = params;
				action = new WriteCharactWithAckAction(txd_charact, rxd_charact, MTBeacon4Protocol.getVersion(),
						new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						System.out.println("onReciveDatas");
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (readinfscallback != null) {
							readinfscallback.onFail(ErroCode.ERROMAP.get("readversionderro"));
						}
					}
				};
				addNewAction(action);
			}

			@Override
			public void onGetPrivateBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				if (!statues) {
					disConnect();
					if (readinfscallback != null) {
						readinfscallback.onFail(ErroCode.ERROMAP.get("readprivateparamsfail"));
					}
					return;
				}

				privatedeviceinf[readid] = params;
				readid++;
				if (readid >= privatedeviceinf.length) { // 读取完毕
					disConnect();
					if (readinfscallback != null) {
						readinfscallback.onSuccess(publicdeviceinf, privatedeviceinf);
					}
					return;
				}

				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.getPrivateBeaconParams(readid), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (readinfscallback != null) {
							readinfscallback.onFail(ErroCode.ERROMAP.get("readprivateparamsfail"));
						}
					}
				};
				addNewAction(action);
			}
		});

		ErroCode result = connect(device, pwd, 1, false, new CallBack() {

			@Override
			public void onConnect(ErroCode errocode) {
				if (errocode.getCode() != 0) {
					if (readinfscallback != null) {
						readinfscallback.onFail(errocode);
					}
					return;
				}
				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.getPublicBeaconParams(), new Option(1000)) {
					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (readinfscallback != null) {
							readinfscallback.onFail(ErroCode.ERROMAP.get("readpublicparamsfail"));
						}
					}

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}
					
				};
				addNewAction(action);

			}

			@Override
			public void reTryConnect(int lasttimes) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onDisConnect(ErroCode errocode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onManualDisConnect(ErroCode errocode) {

			}

			@Override
			public void onDatasRecive(BluetoothGattCharacteristic rxd_charact) {

			}
		});

		return (result.getCode() == 0);
	}

	// 设置通用参数major/minor/uuid
	public boolean setPrivateParamters(final MTBLEDevice device, final String pwd, final MTBeaconInfModel deviceinf,
			final BaseCallBack callback) {
		final MTBeacon4Protocol protocol = new MTBeacon4Protocol();
		protocol.setCallBack(new MTBeacon4Protocol.MTBeacon4ProtocolCallBack() {

			@Override
			public void unKnowDatas(byte[] cmd) {

			}

			@Override
			public void onVerifyPwd(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				if (!statues) {
					disConnect();
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("pwderro"));
					}
					return;
				}
				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.setPrivateBeaconParams(deviceinf), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (callback != null) {
							callback.onFail(ErroCode.ERROMAP.get("setprivateparamsfail"));
						}
					}
				};
				addNewAction(action);
			}

			@Override
			public void onSetPublicBeaconParams(boolean statues, int reason) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSetPrivateBeaconParams(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				disConnect();
				if (!statues) {
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("setprivateparamsfail"));
					}
					return;
				}
				if (callback != null) {
					callback.onSuccess();
				}
			}

			@Override
			public void onSetDeverlop(boolean statues, int reason) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetVersion(String version, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}

			@Override
			public void onGetPublicBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}

			@Override
			public void onGetPrivateBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}
		});

		ErroCode result = connect(device, pwd, 1, false, new CallBack() {

			@Override
			public void onConnect(ErroCode errocode) {
				if (errocode.getCode() != 0) {
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("connnectfail"));
					}
					return;
				}
				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.setPrivateBeaconParams(deviceinf), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (callback != null) {
							callback.onFail(ErroCode.ERROMAP.get("setprivateparamsfail"));
						}
					}
				};
				addNewAction(action);

			}

			@Override
			public void reTryConnect(int lasttimes) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onDisConnect(ErroCode errocode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onManualDisConnect(ErroCode errocode) {

			}

			@Override
			public void onDatasRecive(BluetoothGattCharacteristic rxd_charact) {

			}
		});

		return (result.getCode() == 0);
	}

	// 设置所有参数
	public boolean setPublicParamters(final MTBLEDevice device, final String pwd, final MTBeaconInfModel deviceinf,
			final BaseCallBack callback) {
		final MTBeacon4Protocol protocol = new MTBeacon4Protocol();
		protocol.setCallBack(new MTBeacon4Protocol.MTBeacon4ProtocolCallBack() {

			@Override
			public void unKnowDatas(byte[] cmd) {

			}

			@Override
			public void onVerifyPwd(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				if (!statues) {
					disConnect();
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("pwderro"));
					}
					return;
				}
				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.setPublicBeaconParams(deviceinf), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (callback != null) {
							callback.onFail(ErroCode.ERROMAP.get("setpublicparamsfail"));
						}
					}
				};
				addNewAction(action);
			}

			@Override
			public void onSetPublicBeaconParams(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				if (!statues) {
					disConnect();
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("setpublicparamsfail"));
					}
					return;
				}

				if (deviceinf.getNewpwd() == null) { // 不用设置密码
					disConnect();
					if (callback != null) {
						callback.onSuccess();
					}
					return;
				}

				if (deviceinf.getNewpwd().length() == 0) { // 取消密码
					action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
							MTBeacon4Protocol.setDeverlop('A', null), new Option(1000)) {

						@Override
						public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
							super.onReciveDatas(characteristic, datas);
							protocol.reviceDatas(datas);
						}

						@Override
						public void onSuccess() {
							super.onSuccess();
						}

						@Override
						public void onFail(ErroCode erro) {
							super.onFail(erro);
							disConnect();
							if (callback != null) {
								callback.onFail(ErroCode.ERROMAP.get("setpublicparamsfail"));
							}
						}
					};
					addNewAction(action);
					return;
				}

				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.setDeverlop('B', deviceinf.getNewpwd()), new Option(1000)) { // 设置新密码

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (callback != null) {
							callback.onFail(ErroCode.ERROMAP.get("setpublicparamsfail"));
						}
					}
				};
				addNewAction(action);
				return;

			}

			@Override
			public void onSetPrivateBeaconParams(boolean statues, int reason) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSetDeverlop(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				disConnect();
				if (!statues) {
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("setpublicparamsfail"));
					}
					return;
				}

				if (callback != null) {
					callback.onSuccess();
				}
			}

			@Override
			public void onGetVersion(String version, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}

			@Override
			public void onGetPublicBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}

			@Override
			public void onGetPrivateBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}
		});

		ErroCode result = connect(device, pwd, 1, false, new CallBack() {

			@Override
			public void onConnect(ErroCode errocode) {
				if (errocode.getCode() != 0) {
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("connnectfail"));
					}
					return;
				}

				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.setPublicBeaconParams(deviceinf), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (callback != null) {
							callback.onFail(ErroCode.ERROMAP.get("setpublicparamsfail"));
						}
					}
				};
				addNewAction(action);

			}

			@Override
			public void reTryConnect(int lasttimes) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onDisConnect(ErroCode errocode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onManualDisConnect(ErroCode errocode) {

			}

			@Override
			public void onDatasRecive(BluetoothGattCharacteristic rxd_charact) {

			}
		});

		return (result.getCode() == 0);
	}

	// 设置临时部署
	public boolean setUndevelop(final MTBLEDevice device, final String pwd, final BaseCallBack callback) {
		final MTBeacon4Protocol protocol = new MTBeacon4Protocol();
		protocol.setCallBack(new MTBeacon4Protocol.MTBeacon4ProtocolCallBack() {

			@Override
			public void unKnowDatas(byte[] cmd) {

			}

			@Override
			public void onVerifyPwd(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				if (!statues) {
					disConnect();
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("pwderro"));
					}
					return;
				}
				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.setDeverlop('C', null), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (callback != null) {
							callback.onFail(ErroCode.ERROMAP.get("setdevelopfail"));
						}
					}
				};
				addNewAction(action);
			}

			@Override
			public void onSetPublicBeaconParams(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				return;
			}

			@Override
			public void onSetPrivateBeaconParams(boolean statues, int reason) {
			}

			@Override
			public void onSetDeverlop(boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
				disConnect();
				if (!statues) {
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("setpublicparamsfail"));
					}
					return;
				}

				if (callback != null) {
					callback.onSuccess();
				}
			}

			@Override
			public void onGetVersion(String version, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}

			@Override
			public void onGetPublicBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}

			@Override
			public void onGetPrivateBeaconParams(MTBeaconInfModel params, boolean statues, int reason) {
				action.setStatues(ActionStatues.DONE);
			}
		});

		ErroCode result = connect(device, pwd, 1, false, new CallBack() {

			@Override
			public void onConnect(ErroCode errocode) {
				if (errocode.getCode() != 0) {
					if (callback != null) {
						callback.onFail(ErroCode.ERROMAP.get("connnectfail"));
					}
					return;
				}

				action = new WriteCharactWithAckAction(txd_charact, rxd_charact,
						MTBeacon4Protocol.setDeverlop('C', null), new Option(1000)) {

					@Override
					public void onReciveDatas(BluetoothGattCharacteristic characteristic, byte[] datas) {
						super.onReciveDatas(characteristic, datas);
						protocol.reviceDatas(datas);
					}

					@Override
					public void onSuccess() {
						super.onSuccess();
					}

					@Override
					public void onFail(ErroCode erro) {
						super.onFail(erro);
						disConnect();
						if (callback != null) {
							callback.onFail(ErroCode.ERROMAP.get("setdevelopfail"));
						}
					}
				};
				addNewAction(action);

			}

			@Override
			public void reTryConnect(int lasttimes) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onDisConnect(ErroCode errocode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onManualDisConnect(ErroCode errocode) {

			}

			@Override
			public void onDatasRecive(BluetoothGattCharacteristic rxd_charact) {

			}
		});

		return (result.getCode() == 0);
	}

	private CallBack callback;

	private MTBeaconInfModel publicdeviceinf;
	private MTBeaconInfModel[] privatedeviceinf = new MTBeaconInfModel[MAXPRIVATEID];
	private int readid = 0;

	// 回调
	public static interface CallBack {
		public void onConnect(ErroCode errocode);

		public void reTryConnect(int lasttimes);

		public void onDisConnect(ErroCode errocode);

		public void onManualDisConnect(ErroCode errocode);

		public void onDatasRecive(BluetoothGattCharacteristic rxd_charact);
	}

	// 读取数据回调
	public static interface ReadInfsCallBack {
		public void onSuccess(MTBeaconInfModel publicdeviceinf, MTBeaconInfModel[] privatedeviceinf);

		public void onFail(ErroCode erro);
	}

	// 设置通用参数回调
	public static interface BaseCallBack {
		public void onSuccess();

		public void onFail(ErroCode erro);
	}

	// 发送命令回调
	public static interface SendCmdCallBack {
		public void onSuccess(String ackmsg);

		public void onFail(ErroCode erro);
	}
}
